<?php

namespace IDock\Insurance\ZYIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\ZYIC\ZYICEnums;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumRelationPrinter;
use IDock\Printer\RenewalInquiryPrinter;
use IDock\Printer\VehiclePrinter;

class RenewalInquiryResponse extends AllZYICResponse
{
    public $renewFlag = false;

    public function printerInit(): Response
    {
        $renewalInquiryPrinter = new RenewalInquiryPrinter();

        $data = $this->getBody()['content'] ?? '';
        $biFlag = $data['isRalationBI'] ?? '';
        $ciFlag = $data['isRalationCI'] ?? '';
        if (!empty($ciFlag) or !empty($biFlag)) {
            $this->renewFlag = true;
        }

        $ciData = $data['ci'] ?? [];
        $biData = $data['bi'] ?? [];

        $renewalInquiryPrinter->setRenewalFlg(true);

        //车辆信息
        $vehicle = $data['prpTitemCar'] ?? [];
        $vehiclePrinter = new VehiclePrinter();
        $vehiclePrinter->vehicleName = $vehicle['printModelName'] ?? '';
        $vehiclePrinter->vehicleModelCode = $vehicle['modelCode'] ?? '';
        $vehiclePrinter->vehiclePrice = $vehicle['purchasePrice'] ?? '';
        $vehiclePrinter->vehicleSeat = $vehicle['seatCount'] ?? '';
        $vehiclePrinter->wholeWeight = $vehicle['wholeWeight'] ?? '';
        $vehiclePrinter->vehicleTonnage = round(round($vehicle['tonCount'] ?? '', 3) * 1000, 2);
        $vehiclePrinter->vehicleExhaust = $vehicle['exhaustScale'] ?? '';
        $vehiclePrinter->energyTypeCode = 'E01';
        $vehiclePrinter->energyTypeName = IDockEnums::$energyType[$vehiclePrinter->energyTypeCode] ?? '';
        //车架号
        $renewalInquiryPrinter->vin = $vehicle['vinNo'] ?? '';
        //发动机号
        $renewalInquiryPrinter->engineNo = $vehicle['engineNo'] ?? '';
        $renewalInquiryPrinter->firstRegisterDate = $vehicle['enrollDate'] ?? '';//初登日期
        $renewalInquiryPrinter->licenseNo = $vehicle['licenseNo'] ?? '';

        $insured = $data['prpTinsureds'] ?? [];
        $apply = $insuredMan = $owner = [];
        foreach ($insured as $value) {
            switch ($value['insuredFlag']) {
                case '1':
                    $insuredMan = $value;
                    break;
                case '2':
                    $apply = $value;
                    break;
                case '5':
                    $owner = $value;
                    break;
            }
        }

        //投保人
        $applicantPrinter = new PremiumRelationPrinter();
        $applicantPrinter->name = $apply['insuredName'] ?? '';
        $applicantPrinter->mobile = $apply['mobile'] ?? '';
        $applicantPrinter->address = $apply['postAddress'] ?? '';
        $applicantPrinter->certificateNo = $apply['identifyNumber'] ?? '';
        $applicantPrinter->certificateTypeCode = array_keys(ZYICEnums::$certificateTypeCode, $apply['identifyType'] ?? '')[0] ?? '';
        $applicantPrinter->typeCode = $applicantPrinter->certificateTypeCode == '01' ? '01' : '03';

        $ownerPrinter = new PremiumRelationPrinter();
        $ownerPrinter->name = $owner['insuredName'] ?? $applicantPrinter->name;
        $ownerPrinter->mobile = $owner['mobile'] ?? $applicantPrinter->mobile;
        $ownerPrinter->address = $owner['postAddress'] ?? $applicantPrinter->address;
        $ownerPrinter->certificateNo = $owner['identifyNumber'] ?? $applicantPrinter->certificateNo;
        $ownerPrinter->certificateTypeCode = array_keys(ZYICEnums::$certificateTypeCode, $owner['identifyType'] ?? '')[0] ?? $applicantPrinter->certificateTypeCode;
        $ownerPrinter->typeCode = $ownerPrinter->certificateTypeCode == '01' ? '01' : '03';

        //被保人
        $insuredPrinter = new PremiumRelationPrinter();
        $insuredPrinter->name = $insuredMan['insuredName'] ?? $applicantPrinter->name;
        $insuredPrinter->mobile = $insuredMan['mobile'] ?? $applicantPrinter->mobile;
        $insuredPrinter->address = $insuredMan['postAddress'] ?? $applicantPrinter->address;
        $insuredPrinter->certificateNo = $insuredMan['identifyNumber'] ?? $applicantPrinter->certificateNo;
        $insuredPrinter->certificateTypeCode = array_keys(ZYICEnums::$certificateTypeCode, $insuredMan['identifyType'] ?? '')[0] ?? $applicantPrinter->certificateTypeCode;
        $insuredPrinter->email = $insuredMan['insuredType'] ?? $applicantPrinter->email;
        $insuredPrinter->typeCode = $insuredPrinter->certificateTypeCode == '01' ? '01' : '03';

        $renewalInquiryPrinter->setApplicantPrinter($applicantPrinter);
        $renewalInquiryPrinter->setInsuredPrinter($insuredPrinter);
        $renewalInquiryPrinter->setOwnerPrinter($ownerPrinter);
        $renewalInquiryPrinter->setVehiclePrinter($vehiclePrinter);

        $renewalInquiryPrinter->ciPolicyNo = $ciData['oldPolicyNo'] ?? '';
        $renewalInquiryPrinter->ciStartData = $ciData['startDate'] ?? '';
        $renewalInquiryPrinter->ciStartData = $renewalInquiryPrinter->ciStartData ? date('Y-m-d H:i:s', strtotime($renewalInquiryPrinter->ciStartData)) : '';
        $renewalInquiryPrinter->ciEndData = $ciData['endDate'] ?? '';
        $renewalInquiryPrinter->ciEndData = $renewalInquiryPrinter->ciEndData ? date('Y-m-d H:i:s', strtotime($renewalInquiryPrinter->ciEndData)) : '';
        $renewalInquiryPrinter->biPolicyNo = $biData['oldPolicyNo'] ?? '';//上年商业险保单号
        $renewalInquiryPrinter->biStartData = $biData['startDate'] ?? '';
        $renewalInquiryPrinter->biStartData = $renewalInquiryPrinter->biStartData ? date('Y-m-d H:i:s', strtotime($renewalInquiryPrinter->biStartData)) : '';
        $renewalInquiryPrinter->biEndData = $biData['endDate'] ?? '';
        $renewalInquiryPrinter->biEndData = $renewalInquiryPrinter->biEndData ? date('Y-m-d H:i:s', strtotime($renewalInquiryPrinter->biEndData)) : '';

        $coverageList = $biData['prpTitemKinds'] ?? [];
        foreach ($coverageList as $policyItem) {
            //商业险险种
            $coveragePrinter = new PremiumCoveragePrinter();
            $coverageNo = $policyItem['kindCode'] ?? '';
            $coveragePrinter->coverageCode = array_search($coverageNo, ZYICEnums::$coverageCode);
            if (!$coveragePrinter->coverageCode) {
                continue;
            }
            $coveragePrinter->coverageName = IDockEnums::$coverageCode[$coveragePrinter->coverageCode];
            $coveragePrinter->amount = $policyItem['amount'] ?? '';
            $coveragePrinter->unitAmount = $policyItem['unitAmount'] ?? '';
            $coveragePrinter->premium = $policyItem['premium'] ?? '';
            $renewalInquiryPrinter->setCoveragePrinterList($coveragePrinter);
        }

        $this->printer = $renewalInquiryPrinter;

        return $this;
    }
}
